package com.smarthome.core.account.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShAccountDao;
import com.smarthome.base.service.ShAccountDoorService;
import com.smarthome.base.service.ShAccountService;
import com.smarthome.base.service.ShResidenceService;
import com.smarthome.common.tools.ApiCachedKeysUtil;
import com.smarthome.common.tools.SHACipher;
import com.smarthome.common.tools.cache.JedisUtils;
import com.smarthome.core.account.AccountDoorService;
import com.smarthome.core.account.AccountService;
import com.smarthome.domain.bean.ShAccount;
import com.smarthome.domain.bean.ShAccountDoor;
import com.smarthome.domain.bean.ShResidence;
import com.smarthome.domain.enums.AccountStatus;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * @author zyf
 * @date 2015年9月16日 下午5:28:02
 * @version 1.0
 * @Description: 平台用户操作实现类
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Resource
    private ShAccountService shAccountService;
    @Resource
    private AccountDoorService accountDoorService;
    @Resource
    private ShAccountDao shAccountDao;
    @Resource
    private ShAccountDoorService shAccountDoorService;
    @Resource(name = "shResidenceService")
    private ShResidenceService shResidenceService;

    @Override
    public DataGrid queryDataGrid(ShAccount account, PageHelper page) {
        return shAccountService.queryDataGrid(account, page);
    }

    @Override
    public List<ShAccount> queryAccounts(ShAccount account) {
        return shAccountDao.selectShAccountByCon(account);
    }

    @Override
    public ShAccount queryAccountById(Long id) {
        return shAccountDao.selectShAccountById(id);
    }

    @Override
    public void add(ShAccount account) {
        account.setStatus(AccountStatus.NORMAL.getType());
        shAccountService.saveOrUpdate(account);
        // Long accountId=shAccountDao.lastSequence();
        //
        // //添加小区和业主的映射
        // ShAccountResidence sar=new ShAccountResidence();
        // sar.setAccountId(accountId);
        // sar.setResidenceId(account.getResidenceId());
        // shAccountResidenceService.saveOrUpdate(sar);
        //
        // //添加门牌号和业主的映射
        // ShAccountDoor sad=new ShAccountDoor();
        // sad.setAccountId(accountId);
        // sad.setDoorId(account.getDoorId());
        // shAccountDoorService.saveOrUpdate(sad);
    }

    @Override
    public void edit(ShAccount account) {
        // ShAccount last=shAccountService.selectEntryById(account.getId());
        shAccountService.saveOrUpdate(account);

        // //更改小区和业主的映射
        // ShAccountResidence sar=new ShAccountResidence();
        // sar.setAccountId(account.getId());
        // sar.setResidenceId(last.getResidenceId());
        // List<ShAccountResidence> sars=shAccountResidenceService.selectEntryList(sar);
        // sar=sars.size()==0?sar:sars.get(0);
        // //设置新的小区
        // sar.setResidenceId(account.getResidenceId());
        // shAccountResidenceService.saveOrUpdate(sar);
        //
        // //更改门牌号和业主的映射
        // ShAccountDoor sad=new ShAccountDoor();
        // sad.setAccountId(account.getId());
        // sad.setDoorId(last.getDoorId());
        // List<ShAccountDoor> sads=shAccountDoorService.selectEntryList(sad);
        // sad = sads.size()==0?sad:sads.get(0);
        // //设置新的门牌号
        // sad.setDoorId(account.getDoorId());
        // shAccountDoorService.saveOrUpdate(sad);
    }

    @Override
    public void delete(Long id) {
        ShAccount account = queryAccountById(id);
        account.setStatus(AccountStatus.DELETED.getType());
        shAccountService.saveOrUpdate(account);
    }

    @Override
    public boolean ownerExsit(Long doorId) {
        ShAccount con = new ShAccount();
        List<ShAccount> accs = shAccountDao.selectShAccountByCon(con);
        if (accs.size() > 0)
            return true;
        return false;
    }

    @Override
    public boolean longinNameExist(String loginName) {
        ShAccount con = new ShAccount();
        con.setLoginName(loginName);
        List<ShAccount> accs = shAccountDao.selectShAccountByCon(con);
        if (accs.size() > 0)
            return true;
        return false;
    }

    @Override
    public ShAccount queryByNameAndPwd(String loginName, String password) {
        ShAccount account = new ShAccount();
        account.setLoginName(loginName);
        account.setLoginPwd(password);
        List<ShAccount> accounts = shAccountDao.selectEntryList(account);
        if (null != accounts && accounts.size() > 0)
            return accounts.get(0);
        return null;
    }

    @Override
    public void resetAccountInCache(long accountId) {
        ShAccount account = queryAccountById(accountId);
        List<ShResidence> shResidences = shResidenceService.queryResidencesByAccountId(accountId);
        if (null == shResidences || shResidences.size() == 0) {
            shResidences = new ArrayList<ShResidence>();
            ShResidence shResidence = new ShResidence();
            shResidence.setId(0L);
            shResidence.setName("未设置小区");
            shResidences.add(shResidence);
        }
        account.setResidenceId(shResidences.get(0).getId());
        account.setResidenceName(shResidences.get(0).getName());
        account.setResidences(shResidences);
        ShAccountDoor accountDoor = accountDoorService.getDefaultDoor(account.getId(), account.getResidenceId());
        if (accountDoor != null) {
            account.setDoorId(accountDoor.getDoorId());
            account.setDoorName(accountDoor.getDoorName());
        }
        try {
            String token = SHACipher.encrypt(account.getMobile());
            JedisUtils.set(ApiCachedKeysUtil.getKey(ApiCachedKeysUtil.MODULE_MOBILE_TOKEN, token), account);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

	@Override
	public List<String> getShAccountIds(ShAccount account) {
		return shAccountService.getShAccountIds(account);
	}
}
